package com.hr.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hr.entity.BaseInfo;
import com.hr.entity.User;
import com.hr.service.BaseInfoService;
import com.hr.service.JobService;
import com.hr.service.UserService;
import com.hr.utils.ConstantUtil;
import com.hr.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-20
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/base-info")
public class BaseInfoController {

    @Autowired
    BaseInfoService baseInfoService;
    @Autowired
    UserService userService;

    @GetMapping("baseInfos")
    public R get_baseInfos() {
        List<BaseInfo> baseInfos = baseInfoService.list(null);
        return R.ok().data("baseInfos", baseInfos);
    }

    @GetMapping("baseInfo")
    public R get_baseInfo() {
        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        BaseInfo baseInfo = baseInfoService.getOne(wrapper);
        return R.ok().data("baseInfo", baseInfo);
    }

    @PostMapping("baseInfo")
    public R add_baseInfo(@RequestBody BaseInfo baseInfo) {
        boolean save = baseInfoService.save(baseInfo);
        return save ? R.ok() : R.error();
    }

    @PutMapping("baseInfo")
    public R update_baseInfo(@RequestBody BaseInfo baseInfo) {
        boolean update = baseInfoService.updateById(baseInfo);
        return update ? R.ok() : R.error();
    }

    @DeleteMapping("baseInfo/{id}")
    public R delete_baseInfo(@PathVariable("id") String id) {
        boolean delete = baseInfoService.removeById(id);
        return delete ? R.ok() : R.error();
    }

    @PostMapping("store")
    public R store_baseInfo(@RequestBody BaseInfo baseInfo){
        QueryWrapper<BaseInfo> idcardWrapper = new QueryWrapper();
        idcardWrapper.eq("id_card", baseInfo.getIdCard());
        BaseInfo one = baseInfoService.getOne(idcardWrapper);

        if(one != null && !one.getName().equals(baseInfo.getName())) {
            return R.error().message("身份证号已被使用");
        }

        QueryWrapper<BaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        BaseInfo info = baseInfoService.getOne(wrapper);
        if (info == null){
            baseInfo.setUserId(ConstantUtil.CURRENT_USER);
            boolean save = baseInfoService.save(baseInfo);

            User user = userService.getById(baseInfo.getUserId());
            user.setInfo(1);
            userService.updateById(user);
            return save ? R.ok() : R.error();
        } else {
            Integer id = baseInfo.getId();
            BeanUtils.copyProperties(baseInfo, info);
            info.setId(id);
            boolean update = baseInfoService.updateById(baseInfo);
            return update ? R.ok() : R.error();
        }
    }
}

